package org.jeecg.modules.business.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.business.entity.BusBookingRooms;
import org.jeecg.modules.business.entity.BusRoomBookingOrders;
import org.jeecg.modules.business.service.IBusBookingRoomsService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.business.service.IBusRoomBookingOrdersService;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.mapper.CesRoomsMapper;
import org.jeecg.modules.rooms.service.CesRoomsServiceImpl;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 预定订单关联房间
 * @Author: jeecg-boot
 * @Date:   2023-03-25
 * @Version: V1.0
 */
@Api(tags="预定订单关联房间")
@RestController
@RequestMapping("/business/busBookingRooms")
@Slf4j
public class BusBookingRoomsController extends JeecgController<BusBookingRooms, IBusBookingRoomsService> {
	@Autowired
	private IBusBookingRoomsService busBookingRoomsService;
	@Resource
	private CesRoomsMapper roomsMapper;
	 @Resource
	 private IBusRoomBookingOrdersService roomBookingOrdersService;
	 @Resource
	 private CesRoomsServiceImpl roomsService;
	/**
	 * 分页列表查询
	 *
	 * @param busBookingRooms
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "预定订单关联房间-分页列表查询")
	@ApiOperation(value="预定订单关联房间-分页列表查询", notes="预定订单关联房间-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<BusBookingRooms>> queryPageList(BusBookingRooms busBookingRooms,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<BusBookingRooms> queryWrapper = QueryGenerator.initQueryWrapper(busBookingRooms, req.getParameterMap());
		Page<BusBookingRooms> page = new Page<BusBookingRooms>(pageNo, pageSize);
		IPage<BusBookingRooms> pageList = busBookingRoomsService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param busBookingRooms
	 * @return
	 */
	@AutoLog(value = "预定订单关联房间-添加")
	@ApiOperation(value="预定订单关联房间-添加", notes="预定订单关联房间-添加")
	//@RequiresPermissions("business:bus_booking_rooms:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody BusBookingRooms busBookingRooms) {
		busBookingRoomsService.save(busBookingRooms);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param busBookingRooms
	 * @return
	 */
	@AutoLog(value = "预定订单关联房间-编辑")
	@ApiOperation(value="预定订单关联房间-编辑", notes="预定订单关联房间-编辑")
	//@RequiresPermissions("business:bus_booking_rooms:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody BusBookingRooms busBookingRooms) {
		busBookingRoomsService.updateById(busBookingRooms);
		return Result.OK("编辑成功!");
	}

	 /**
	  *  批量排房
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "预定订单关联房间-批量排房")
	 @ApiOperation(value="预定订单关联房间-批量排房", notes="预定订单关联房间-批量排房")
	 @RequestMapping(value = "/editBatch", method = {RequestMethod.POST})
	 public Result<String> editBatch(@RequestBody List<String> ids,String hotelId) {
		 if (CollUtil.isEmpty(ids)){
			 return Result.error("参数异常");
		 }
		 LambdaQueryWrapper<BusBookingRooms> wrapper = new LambdaQueryWrapper<>();
		 wrapper.eq(BusBookingRooms::getRoomStatus, 1);
		 wrapper.in(BusBookingRooms::getId, ids);
		 List<BusBookingRooms> bookingRooms = busBookingRoomsService.list(wrapper);
		 if (ids.size() != bookingRooms.size()){
			 return Result.error("未找到预约单");
		 }
		 BusRoomBookingOrders bookingOrder = roomBookingOrdersService.getById(bookingRooms.get(0).getBookingOrdersId());
		 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		 List<CesRooms> cesRooms = roomsMapper.canUseRooms(hotelId, formatter.format(bookingOrder.getArrivalTime()), formatter.format(bookingOrder.getDueOutTime()));
		 ArrayList<CesRooms> useRoomList = new ArrayList<>();
		 bookingRooms.forEach(e -> {
			 cesRooms.forEach(room ->{
				 if (room.getLayoutId().equals(e.getRoomLayoutId()) && !useRoomList.contains(room)){
					 e.setRoomId(room.getId());
					 useRoomList.add(room);
				 }
			 });
		 });
		 busBookingRoomsService.updateBatchById(bookingRooms);
		 roomsService.getReserveRoom();
		 return Result.OK("编辑成功!");
	 }

	 /**
	  *  设置为主房
	  *
	  * @param busBookingRooms
	  * @return
	  */
	 @AutoLog(value = "预定关联房间-设置为主房")
	 @ApiOperation(value="预定关联房间-设置为主房", notes="预定关联房间-设置为主房")
	 //@RequiresPermissions("business:bus_booking_rooms:edit")
	 @RequestMapping(value = "/set-main", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> setMain(@RequestBody BusBookingRooms busBookingRooms, String livingOrderId) {
		 busBookingRoomsService.setMain(busBookingRooms.getId(), livingOrderId);
		 return Result.OK("编辑成功!");
	 }

	 /**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */	@AutoLog(value = "预定订单关联房间-通过id删除")
	@ApiOperation(value="预定订单关联房间-通过id删除", notes="预定订单关联房间-通过id删除")
	//@RequiresPermissions("business:bus_booking_rooms:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		busBookingRoomsService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "预定订单关联房间-批量删除")
	@ApiOperation(value="预定订单关联房间-批量删除", notes="预定订单关联房间-批量删除")
	//@RequiresPermissions("business:bus_booking_rooms:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.busBookingRoomsService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "预定订单关联房间-通过id查询")
	@ApiOperation(value="预定订单关联房间-通过id查询", notes="预定订单关联房间-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<BusBookingRooms> queryById(@RequestParam(name="id",required=true) String id) {
		BusBookingRooms busBookingRooms = busBookingRoomsService.getById(id);
		if(busBookingRooms==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(busBookingRooms);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param busBookingRooms
    */
    //@RequiresPermissions("business:bus_booking_rooms:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, BusBookingRooms busBookingRooms) {
        return super.exportXls(request, busBookingRooms, BusBookingRooms.class, "预定订单关联房间");
    }

}
